
from clickhouse_driver import Client
import pandas as pd
class ClickHouseClient:
    def __init__(self, host='172.29.151.181', port=9000, user='default',
                 password='Keepgo123@cys', database='db_cfx'):
        self.client = Client(
            host=host,
            port=port,
            user=user,
            password=password,
            database=database,
            settings={'use_numpy': True}  # 可选，提高性能
        )
        self.database = database

    def query(self, query: str, params: dict = None) -> pd.DataFrame:

        try:
            result = self.client.query_dataframe(query, params)
            return result
        except Exception as e:
            return pd.DataFrame()

    def execSql(self, update_sql):

        # 3. 执行 UPDATE 语句
        try:

            self.client.execute(update_sql)
            print("UPDATE 语句已提交（异步执行中）")
        except Exception as e:
            print(f"执行失败：{e}")



# 使用示例
if __name__ == "__main__":

    # 方法1: 常规使用
    client = ClickHouseClient()

    with open('ch_sql.txt', 'r', encoding='utf-8') as f:
        #逐行遍历文件，每次迭代返回一行（包含末尾的换行符 '\n'）
        for line in f:
            # 处理每行内容（示例：打印行内容，去除末尾换行符可用 strip()）
            print(line.strip())  # strip() 移除首尾空白（包括 '\n'）

            data = client.execSql(line.strip())


